package com.hh.erp.util.http;

import com.hh.erp.entity.vo.UserInfoVo;
import com.hh.erp.util.Convert;
import com.hh.erp.util.JsonUtil;
import com.hh.erp.util.RedisUtil;
import com.hh.erp.util.page.PageInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class LoginUtil {
    // 获取token
    public static String getToken() {
        HttpServletRequest request = ServletUtils.getRequest();
        String token = request.getHeader("X-Token");
        return token;
    }

    /**
     * 获取当前用户的登录信息
     * @return
     */
    public static UserInfoVo getUserInfo() {
        String token = LoginUtil.getToken();
        String jsonStr = RedisUtil.getValue(token);
        UserInfoVo userInfoVo = JsonUtil.toObject(RedisUtil.getValue(token),UserInfoVo.class);
        return userInfoVo;
    }

    //获取分页信息
    public static PageInfo getPageInfo() {
        HttpServletRequest request = ServletUtils.getRequest();
        Integer pageNum = Convert.toInt(request.getParameter(Constants.PAGE_NUM));
        Integer pageSize = Convert.toInt(request.getParameter(Constants.PAGE_SIZE));
        return  PageInfo.start(pageNum,pageSize);
    }
}
